package com.bjc.core.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.ResultVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.core.bean.request.UserReqVO;
import com.bjc.core.bean.response.UserVO;
import com.bjc.core.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户控制器
 * 
 * @author 许自辉
 * @date 2017年8月4日
 */
@RestController
@RequestMapping("api2/cloud")
// @PermissionPath("系统/用户")
@Api(value = "user-api", tags = { "用户相关接口" })
public class UserController {

	@Autowired
	private IUserService userService;

	@ApiOperation("分页查询用户")
	@GetMapping("user/findPage")
	public ResultVO getUserPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			UserReqVO userReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		return ResultGenerator.getSuccess(new PageInfo<UserVO>(userService.getList(userReqVO)));
	}

	@ApiOperation("获取当前登录用户")
	@GetMapping("user/findCurrent")
	public ResultVO getUserPage() {
		Long currentWorkId = (Long) SecurityUtils.getSubject().getSession().getAttribute("currentWorkId");
		System.out.println("获取当前登录用户"+currentWorkId);
		UserVO userVO = new UserVO();
		userVO.setUserAccount("123456");
		List<String> roleList = new ArrayList<>();
		roleList.add("systemAdmin");
		userVO.setRoleList(roleList);
		return ResultGenerator.getSuccess(userVO);
	}

	@ApiOperation("添加用户")
	@PostMapping("user/create")
	public ResultVO createUser(@RequestBody UserReqVO userReqVO) {
		int count = userService.insert(userReqVO);
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("更新用户")
	@PutMapping("user/update")
	public ResultVO updateUser(@RequestBody UserReqVO userReqVO) {
		int count = userService.update(userReqVO);
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("删除用户")
	@DeleteMapping("user/delete/{id}")
	public ResultVO deleteUserById(@ApiParam(value = "用户id", required = true) @PathVariable("id") Integer id) {
		int count = userService.deleteById(id);
		return ResultGenerator.getDeleteSuccess(count);
	}
}
